package com.gblfy.controller;

import com.gblfy.service.IFlowService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gblfy
 * @ClassNme FlowController
 * @Description TODO
 * @Date 2019/11/4 13:58
 * @version1.0
 */
@Controller
@RequestMapping(value = "flow")
public class FlowController {
    /**
     * 日志类
     */
    private static final Logger log = LoggerFactory.getLogger(FlowController.class);
    /**
     * 流程处理服务
     */
    @Autowired
    private IFlowService flowService;

    @RequestMapping("/create")
    @ResponseBody
    public Map<String, Object> createFlow() {
        Map<String, Object> res = new HashMap<>();
        Map<String, Object> data = new HashMap<>();

        String flowPath ="C:\\Users\\gblfy\\Desktop\\Workflow\\flowablestudy\\flowablelech12\\src\\main\\resources\\processes\\测试BPMN模型3.bpmn20.xml";

        Map<String, Object> createRes = flowService.createFlow(flowPath);

        if (null == createRes) {
            res.put("msg", "创建流程失败");
            res.put("res", "0");
            res.put("data", data);
            return res;
        }
        List<Process> processes = (List<Process>) createRes.get("processes");

        ArrayList<String> ids = new ArrayList<>();
        for (Process process : processes) {
            ids.add(process.getId());
        }
        data.put("processKeys", ids);
        data.put("deployId", ((Deployment) createRes.get("deployment")).getId());
        res.put("data", data);
        res.put("msg", "创建流程成功");
        res.put("res", "1");
        return res;
    }


    @RequestMapping("/start")
    @ResponseBody
    public Map<String, Object> startFlow(@RequestBody @RequestParam(required = false) Map<String, String> paras) {
        Map<String, Object> res = new HashMap<>();
        Map<String, String> data = new HashMap<>();

        if (MapUtils.isEmpty(paras)) {
            res.put("msg", "启动流程失败");
            res.put("res", "0");
            res.put("data", data);
            return res;
        }

        String processKey = paras.get("processKey");
        if (StringUtils.isEmpty(processKey)) {
            res.put("msg", "启动流程失败");
            res.put("res", "0");
            res.put("data", data);
            return res;
        }

        Map<String, Object> flowParas = new HashMap<>();
        flowParas.putAll(paras);
        ProcessInstance processInstance = flowService.strartFlow(processKey, flowParas);
        if (null == processInstance) {
            res.put("msg", "启动流程失败");
            res.put("res", "0");
            res.put("data", data);
            return res;
        }
        data.put("processId", processInstance.getId());
        res.put("msg", "启动流程成功");
        res.put("res", "1");
        res.put("data", data);
        return res;
    }

}
